package com.ozy.kxreader.mvp.presenter

import com.chad.library.adapter.base.entity.SectionEntity
import com.ozy.kxreader.app.RxLifecycleUtils
import com.ozy.kxreader.base.BasePresenter
import com.ozy.kxreader.di.scope.ActivityScope
import com.ozy.kxreader.mvp.contract.CategoryContract
import com.ozy.kxreader.mvp.model.entity.Category
import com.ozy.kxreader.mvp.model.entity.Section
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.ArrayList
import javax.inject.Inject

/**
 * Created by 欧源 on 2018/1/31.
 */
@ActivityScope
class CategoryPresenter
@Inject constructor(model: CategoryContract.Model, view: CategoryContract.View)
    : BasePresenter<CategoryContract.Model, CategoryContract.View>(model, view) {


    fun getCategoryList() {
        mModel.getCategoryList()
                .subscribeOn(Schedulers.io())
                .retry()
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(
                        { res ->
                            val data = ArrayList<SectionEntity<Category>>()
                            data.add(Section(true, "男生", "male"))
                            val male = res.male
                            for (classify in male) {
                                data.add(Section(classify))
                            }
                            val female = res.female
                            data.add(Section(true, "女生","female"))
                            for (classify in female) {
                                data.add(Section(classify))
                            }

                            val picture = res.picture
                            data.add(Section(true, "动漫","pictrue"))
                            for (classify in picture) {
                                data.add(Section(classify))
                            }

                            val press = res.press
                            data.add(Section(true, "出版","press"))
                            for (classify in press) {
                                data.add(Section(classify))
                            }
                            mRootView.showCategoryList(data)
                        },
                        { e ->
                            e.printStackTrace()
                        })
    }


}